package cn.lystudio.rohmtimeopwide_background_v11.controller.webApi;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.PageUtil;
import cn.lystudio.rohmtimeopwide_background_v11.entity.dto.AbHandleItemDTO;
import cn.lystudio.rohmtimeopwide_background_v11.entity.po.AbHandleItemPO;
import cn.lystudio.rohmtimeopwide_background_v11.entity.vo.LayuiPageApiResponse;
import cn.lystudio.rohmtimeopwide_background_v11.exceptions.JsonException;
import cn.lystudio.rohmtimeopwide_background_v11.service.AbHandleItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@SaCheckLogin
@RequestMapping("/webapi/abHandleItem")
@Slf4j
public class AbHandleItemController {
    private AbHandleItemService abHandleItemService;

    @Autowired
    @Lazy
    public void setAbHandleItemService(AbHandleItemService abHandleItemService) {
        this.abHandleItemService = abHandleItemService;
    }

    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @GetMapping("/findAll")
    public LayuiPageApiResponse findAll(Integer page, Integer limit, @RequestParam(name = "keyword",defaultValue = "")String keyword){
        List<String> roleList = StpUtil.getRoleList(StpUtil.getLoginIdAsString());
        if (roleList.size() != 1) throw new JsonException(500, "权限异常");
        PageUtil.setFirstPageNo(1);
        List<AbHandleItemDTO> all = abHandleItemService.getAll(keyword);
        if(CollUtil.isEmpty(all)) return LayuiPageApiResponse.ofSuccessData(null,0);
        return LayuiPageApiResponse.ofSuccessData(CollUtil.page(page,limit,all),all.size());
    }

    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @PostMapping("/uptEnabled")
    public SaResult uptEnabled(Integer abHandleItemID, Boolean enabled) {
        return SaResult.data(abHandleItemService.uptAbHandleItemEnabled(abHandleItemID,enabled));
    }

    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @PostMapping("/delAbHandleItem")
    public SaResult delAbHandleItem(Integer abHandleItemID){
        return SaResult.data(abHandleItemService.delAbHandleItem(abHandleItemID));
    }

    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @PostMapping("/addAbHandleItem")
    public SaResult addAbHandleItem(@RequestBody AbHandleItemPO abHandleItemPO){
        return  SaResult.data(abHandleItemService.addAbHandleItem(abHandleItemPO));
    }

    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @PostMapping("/uptAbHandleItem")
    public SaResult uptAbHandleItem(@RequestBody AbHandleItemPO abHandleItemPO){
        return  SaResult.data(abHandleItemService.uptAbHandleItem(abHandleItemPO));

    }

    @GetMapping("/getAllOfSecDept")
    public SaResult getAllOfSecDept(Integer secDeptID){
        return SaResult.data(abHandleItemService.getAllOfSecDept(secDeptID));
    }


}
